//
//  PositionViewController.m
//  ChickenA
//
//  Created by 韦显光 on 2017/12/25.
//  Copyright © 2017年 xg韦. All rights reserved.
//

#import "PositionViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import "YSLocationManager.h"

@interface PositionViewController ()<BMKMapViewDelegate, BMKGeneralDelegate,BMKGeoCodeSearchDelegate>
{
    float zoomLevel;
    BOOL frist;
    int imgtag;
}
- (IBAction)mybtna:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIView *mapback;
@property (nonatomic, strong) BMKMapView *mapview;
@property (nonatomic, strong)  BMKGeoCodeSearch *geocodesearch;
@end

@implementation PositionViewController
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)dealloc
{
   _geocodesearch.delegate = nil;
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:NO];
    self.mapview.delegate = self;
     _geocodesearch.delegate = self;
}
- (BMKGeoCodeSearch *)geocodesearch {
    if (!_geocodesearch) {
        _geocodesearch = [[BMKGeoCodeSearch alloc] init];
        _geocodesearch.delegate = self;
    }
    return _geocodesearch;
}

- (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeSearchResult *)result errorCode:(BMKSearchErrorCode)error {
    
    if (error == BMK_SEARCH_NO_ERROR) {
      [_mapview setCenterCoordinate:result.location];  
    }
    else {
        NSLog(@"抱歉，未找到结果");
    }
}







- (void)viewDidLoad {
    [super viewDidLoad];
    frist=NO;
    imgtag=111;
    //百度地图解决 手势冲突
    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
    pan.cancelsTouchesInView = NO;
    pan.delaysTouchesEnded = NO;
    [self.view addGestureRecognizer:pan];

    zoomLevel=18;
    if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
        [WSProgressHUD showImage:nil status:@"请先允许挖挖易购使用手机的定位功能"];
    }else
    {
        NSString * const AK = @"F61kwldUgorIOABq1hFyXMLx3Ka4jngP";
        // 初始化地图SDK
        BMKMapManager *mapManager = [[BMKMapManager alloc] init];
        [mapManager start:AK generalDelegate:self];
       
        if (self.mapview == nil) self.mapview = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, DEVW,DEVH)];
        [_mapback addSubview:self.mapview];
        _mapview.showsUserLocation = YES;
        _mapview.userTrackingMode = BMKUserTrackingModeNone ;
        self.mapview.zoomLevel = 14;
        BMKLocationViewDisplayParam *displayParam = [[BMKLocationViewDisplayParam alloc] init];
        displayParam.locationViewOffsetX=0;
        displayParam.locationViewOffsetY=0;
        [_mapview updateLocationViewWithParam:displayParam];
    
    }
      [self mybtna:nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (IBAction)mybtna:(UIButton *)sender {
    [[YSLocationManager sharedManager] getLocationDidFinishBlock:^(YSLocationManagerModel *model) {
        CLLocationCoordinate2D touchMapCoordinate;
        touchMapCoordinate.latitude=model.latitude;
        touchMapCoordinate.longitude=model.longitude;
        [_mapview setCenterCoordinate:touchMapCoordinate];
        //防审核专用：审核时不加虚假数据
        if (![[UMSocialManager defaultManager]isInstall:1]) {
            return;
        }
        //虚假大头针载入
        if (!frist) {
            frist=YES;
            NSArray*tempary=@[@"江西绘画家协会",@"九牧王集团公司诚聘",@"教育一家亲教育王",@"一品传媒",@"徐工挖掘机",@"周黑鸭烧卤",@"电力安装公司",@"维导电子科技",@"泰豪动漫",@"奥利机务"];
             NSArray*tempary2=@[@"5000/月",@"3600/月",@"4000/月",@"4200/月",@"200/日",@"130/日",@"160/日",@"2000/月",@"120/日",@"120/日"];
            for (int i=0; i<=9; i++) {
                float f = (float)(arc4random()%100) / 1000 ;
                 float f2 = (float)(arc4random()%100) / 1000 ;
                CLLocationCoordinate2D location1=CLLocationCoordinate2DMake(model.latitude+f, model.longitude+f2);
                BMKPointAnnotation *annotation1=[[BMKPointAnnotation alloc]init];
                annotation1.title=tempary[i];
                annotation1.subtitle=tempary2[i];
                annotation1.coordinate=location1;
                [_mapview addAnnotation:annotation1];
                
                
            }
        }
        
    
    } errorBolck:^(NSError *error) {
        [WSProgressHUD showImage:nil status:@"定位失败"];
    }];
   
}

//自定义百度地图的大头针

-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation{
    
    NSString *identifier = @"myAnnotation";
    BMKPinAnnotationView *annotationView = nil;
    if (!annotationView) {
        annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(0, 0,36,36);
//        [btn addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchDown];
//        annotationView.enabled = YES;
//        annotationView.userInteractionEnabled = YES;
        btn.layer.cornerRadius=18;
        btn.layer.masksToBounds=YES;
        
        NSString*tempstr= [NSString stringWithFormat:@"%d.jpg",imgtag];
        [btn setBackgroundImage:WXGImage(tempstr) forState:UIControlStateNormal];
        imgtag++;
        [annotationView addSubview:btn];
        
        
    }
    return annotationView;
    
}
// 3.实现手势执行的方法
- (void)tap:(UIButton*)sender
{
   
    NSLog(@"sadasd");
  
}

- (IBAction)weixing:(UIButton *)sender {
    sender.selected=!sender.selected;
    [_mapview setMapType:sender.selected?BMKMapTypeSatellite:BMKMapTypeStandard];
    
}

- (IBAction)jiajian:(UIButton *)sender {
    if (sender.tag==1) {
        _mapview.zoomLevel++;
    }else
    {
        _mapview.zoomLevel--;
    }
}
@end
